//
//  DHStatusCell.swift
//  Weibo
//
//  Created by Holfioo on 15/9/7.
//  Copyright © 2015年 Holfioo. All rights reserved.
//

import UIKit

let DHStatusCellMargin:CGFloat = 12
let DHStatusIconWH: CGFloat = 35

let DHPictureItemWH: CGFloat = 90
let DHPictureItemMargin: CGFloat = 10
// 每行最多照片数
let DHPictuerMaxRowCount: CGFloat = 3
// 配图的最大尺寸
let DHPictuerMaxWH = DHPictuerMaxRowCount * DHPictureItemWH + DHPictureItemMargin * (DHPictuerMaxRowCount - 1)

class DHStatusCell: UITableViewCell {
    // 图片的约束
    var pictureViewWidthCons: NSLayoutConstraint?
    var pictureViewHeightCons: NSLayoutConstraint?
    var pictureViewTopCons: NSLayoutConstraint?
    
    // 视图模型
    var statusViewModel: DHStatusViewModel? {
        didSet {
            // 视图模型
            topView.statusViewModel = statusViewModel
            labText.text = statusViewModel?.status.text
            // cell 的数据模型 和 picture的数据模型进行统一
            pictureView.statusViewModel = statusViewModel
            
            // 设置配图的大小.计算后的结果赋值给当前的变量
            pictureViewWidthCons?.constant = pictureView.bounds.width
            pictureViewHeightCons?.constant = pictureView.bounds.height
//            pictureViewHeightCons?.constant = DHPictureItemWH * CGFloat(random() % 4)
            // 根据是否有图片。决定顶部约束,如果为0 代表顶部约束为0
            pictureViewTopCons?.constant = statusViewModel?.thumbnailUrls?.count == 0 ? 0 : DHStatusCellMargin
        }
    }
    
    // 计算视图模型对应的行高
    func rowHeight(viewModel: DHStatusViewModel) -> CGFloat {
        // 调用模型的 didSet方法
        statusViewModel = viewModel
        // 行高改变的时候 自行计算
        layoutIfNeeded()
        // 返回底部视图的最大Y值。
        return CGRectGetMaxY(bottomView.frame)
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 布局UI
     func setupUI() {
        backgroundColor = UIColor.whiteColor()
        // 增加分割视图
        let TopSep = UIView()
        TopSep.backgroundColor = UIColor(white: 0.85, alpha: 1.0)
        
        // 增加到试图中
        contentView.addSubview(TopSep)
        contentView.addSubview(topView)
        contentView.addSubview(labText)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        let w = UIScreen.mainScreen().bounds.width
        
        TopSep.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: w, height: 10))
        topView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: TopSep, size: CGSize(width: w, height: DHStatusIconWH + DHStatusCellMargin))
        // 设置文本约束
        labText.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: DHStatusCellMargin, y: DHStatusCellMargin))
        
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: w, height: 44), offset: CGPoint(x: -DHStatusCellMargin, y: DHStatusCellMargin))
       // 自动计算行高 需要约束底部
        // bottomView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: contentView, size: nil)
    }
    
    // 懒加载试图控件
    private lazy var topView: DHStatusCellTopView = DHStatusCellTopView()
    lazy var bottomView: DHStatusCellBottomView = DHStatusCellBottomView()
    // 中间的文本标签。以后还会改
    lazy var labText: UILabel = UILabel(title: nil, color: UIColor.darkGrayColor(), fontSize: 15, layoutWidth: UIScreen.mainScreen().bounds.width - 2*DHStatusCellMargin)
    lazy var pictureView: DHStatusPictureView = DHStatusPictureView()
}
